﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LocationBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILocationDA locationDA = DataAccess.CreateLocationDA();
        private PubParasInfo pubParasInfo;
        private static readonly IZoneDA zoneDA = DataAccess.CreateZoneDA();

        public LocationBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LocationBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> locationID)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<LocationInfo> locationInfos = new List<LocationInfo>();
                LocationInfo locationInfo = null;
                foreach (string id in locationID)
                {
                    locationInfo = locationDA.GetLocationByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    locationInfos.Add(locationInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (locationDA.DeleteLocation(dataBase, tran, locationID, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < locationID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<LocationInfo>(dataBase, tran, locationInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LOCATION", this.dataBaseID);
                        temp = temp + locationID[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return locationDA.GetAllLocation(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string locationID)
        {
            return locationDA.GetLocationByID(locationID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            LocationQueryEntity locationQuery = obj as LocationQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(locationQuery.IsActive))
            {
                sqlWhere.Add(" loc.IS_ACTIVE " + locationQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(locationQuery.Loc))
            {
                sqlWhere.Add(" loc.LOC " + locationQuery.Loc);
            }
            if (!string.IsNullOrEmpty(locationQuery.ZoneID))
            {
                sqlWhere.Add(" loc.ZONE_ID " + locationQuery.ZoneID);
            }
            return locationDA.GetLocationByQueryList(sqlWhere, locationQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string locationID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocationInfo location = obj as LocationInfo;
                if (locationDA.CheckLocationIDUnique(dataBase, tran, location.Loc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2330_001", this.pubParasInfo.UserLanguage, new List<string> { location.Loc });
                }
                ZoneInfo zoneInfo = zoneDA.GetZoneByID(dataBase, tran, location.ZoneID, this.dataBaseID[0]);
                if (!zoneDA.CheckZoneIDUnique(dataBase, tran, location.ZoneID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2330_002", this.pubParasInfo.UserLanguage, new List<string> { location.ZoneID });
                }
                location.UpdatedBy = location.CreatedBy;
                location.CreatedDate = DateTime.Now;
                location.UpdatedDate = DateTime.Now;
                if (locationDA.InsertLocation(dataBase, tran, location, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocationInfo>(dataBase, tran, null, location, AuditAction.Insert, this.pubParasInfo.UserID, "LOCATION", this.dataBaseID);
                    locationID = location.Loc;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locationID;
        }

        public string LocImportByData(string[][] ImportByData, string IsUpdate)
        {
            Exception ex;
            string LocIDTrue = string.Empty;
            string LocIDFalse = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 1; ImportByData.Length > i; i++)
                {
                    LocationInfo locationInfo = new LocationInfo();
                    for (int n = 0; ImportByData[i].Length > n; n++)
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(ImportByData[i][n]))
                            {
                                switch (ImportByData[0][n])
                                {
                                    case "货位代码":
                                    {
                                        locationInfo.Loc = ImportByData[i][n];
                                        continue;
                                    }
                                    case "路线顺序":
                                    {
                                        locationInfo.LogicalLoc = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "储区":
                                    {
                                        locationInfo.ZoneID = ImportByData[i][n];
                                        continue;
                                    }
                                    case "货位用途":
                                    {
                                        locationInfo.LocUsage = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "货位种类":
                                    {
                                        locationInfo.LocCategory = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "货位属性":
                                    {
                                        locationInfo.LocFlag = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "周转类型":
                                    {
                                        locationInfo.Abc = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "处理方式":
                                    {
                                        locationInfo.LocHandling = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "货品混放":
                                    {
                                        locationInfo.IsMultiSku = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "批次混放":
                                    {
                                        locationInfo.IsMultiLot = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                    case "长":
                                    {
                                        locationInfo.Length = Convert.ToSingle(ImportByData[i][n]);
                                        continue;
                                    }
                                    case "宽":
                                    {
                                        locationInfo.Width = Convert.ToSingle(ImportByData[i][n]);
                                        continue;
                                    }
                                    case "高":
                                    {
                                        locationInfo.Height = Convert.ToSingle(ImportByData[i][n]);
                                        continue;
                                    }
                                    case "容积":
                                    {
                                        locationInfo.CubicCapacity = Convert.ToDouble(ImportByData[i][n]);
                                        continue;
                                    }
                                    case "承重":
                                    {
                                        locationInfo.WeightCapacity = Convert.ToDouble(ImportByData[i][n]);
                                        continue;
                                    }
                                    case "托盘数量":
                                    {
                                        locationInfo.StackLimit = Convert.ToInt32(ImportByData[i][n]);
                                        continue;
                                    }
                                    case "创建人":
                                    {
                                        locationInfo.CreatedBy = string.IsNullOrEmpty(ImportByData[i][n]) ? "" : ImportByData[i][n];
                                        continue;
                                    }
                                }
                            }
                        }
                        catch (Exception exception1)
                        {
                            ex = exception1;
                            if (!string.IsNullOrEmpty(locationInfo.Loc))
                            {
                                LocIDFalse = LocIDFalse + ex.Message + locationInfo.Loc + "（数据内容异常,导入失败）;<br/>";
                            }
                        }
                    }
                    locationInfo.IsActive = "Y";
                    locationInfo.Status = "OK";
                    locationInfo.IsLoseID = "";
                    locationInfo.SystemCreated = "N";
                    locationInfo.UpdatedBy = locationInfo.CreatedBy;
                    if (!string.IsNullOrEmpty(locationInfo.Loc))
                    {
                        try
                        {
                            if (string.IsNullOrEmpty(locationInfo.LogicalLoc))
                            {
                                locationInfo.LogicalLoc = locationInfo.Loc;
                            }
                            if (!zoneDA.CheckZoneIDUnique(dataBase, tran, locationInfo.ZoneID, this.dataBaseID[0]))
                            {
                                LocIDFalse = LocIDFalse + locationInfo.Loc + PublicMethod.GetInstance().GetGlobalMsgResource("C003", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                            else if (locationDA.CheckLocationIDUnique(dataBase, tran, locationInfo.Loc, this.dataBaseID[0]))
                            {
                                if (IsUpdate == "Y")
                                {
                                    if (locationDA.UpdateLocation(dataBase, tran, locationInfo, this.dataBaseID) > 0)
                                    {
                                        LocIDFalse = LocIDFalse + locationInfo.Loc + PublicMethod.GetInstance().GetGlobalMsgResource("C004", this.pubParasInfo.UserLanguage) + "<br/>";
                                    }
                                }
                                else
                                {
                                    LocIDFalse = LocIDFalse + locationInfo.Loc + PublicMethod.GetInstance().GetGlobalMsgResource("C005", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                            }
                            else if (locationDA.InsertLocation(dataBase, tran, locationInfo, this.dataBaseID[0]) > 0)
                            {
                                LocIDTrue = LocIDTrue + locationInfo.Loc + ";";
                            }
                        }
                        catch (Exception exception2)
                        {
                            ex = exception2;
                            if (!string.IsNullOrEmpty(locationInfo.Loc))
                            {
                                string CS00004 = LocIDFalse;
                                LocIDFalse = CS00004 + locationInfo.Loc + PublicMethod.GetInstance().GetGlobalMsgResource("C006", this.pubParasInfo.UserLanguage) + ex.Message + "<br/>";
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception exception3)
            {
                ex = exception3;
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return LocIDFalse;
        }

        public string Update(object obj)
        {
            string locationID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocationInfo location = obj as LocationInfo;
                location.UpdatedDate = DateTime.Now;
                if (!zoneDA.CheckZoneIDUnique(dataBase, tran, location.ZoneID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2330_002", this.pubParasInfo.UserLanguage, new List<string> { location.ZoneID });
                }
                LocationInfo oldLocationInfo = locationDA.GetLocationByID(dataBase, tran, location.Loc, this.dataBaseID[0], this.pubParasInfo);
                if (locationDA.UpdateLocation(dataBase, tran, location, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocationInfo>(dataBase, tran, oldLocationInfo, location, AuditAction.Update, this.pubParasInfo.UserID, "LOCATION", this.dataBaseID);
                    locationID = location.Loc;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locationID;
        }
    }
}

